#!/usr/bin/env python

import sqlalchemy as sa
import sqlalchemy.orm as sao

from datetime import datetime
from model import DeclarativeBase

__all__ = ['Host']

class Host(DeclarativeBase):
    """Host class
    
    Attributes:
        id: An integer indicating the primary key.
        name: A string represent name of booru.
        url: A string represent the home url.
        username: A string of username.
        password: A string of hashed password.
        hashFormat: A string define the format for password.
        type: A string indicating booru type.
        status: A string determine the livelihood of the site. If status is Dead
            the host will be ignored when updating post or downloading image.
        api: A string for post rest resource. Meobooru using post whereas
            danbooru using posts (notice the s). This field is ignored for
            gelbooru
    """
    
    __tablename__ = 'hosts'
    
    TYPE_DANBOORU = 'danbooru'
    TYPE_GELBOORU = 'gelbooru'
    TYPE_MOEBOORU = 'moebooru'
    
    STATUS_ACTIVE = 'Active'
    STATUS_DEAD = 'Dead'
    
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(32), nullable=False, unique=True, index=True)
    url = sa.Column(sa.String(32), nullable=False, unique=True, index=True)
    username = sa.Column(sa.String(32))
    password = sa.Column(sa.String(40))
    hashFormat = sa.Column(sa.String(64))
    type = sa.Column(sa.String(8))
    status = sa.Column(sa.String(6))
    api = sa.Column(sa.String(8))
    createdAt = sa.Column(sa.DateTime, default=datetime.now)
    updatedAt = sa.Column(sa.DateTime, default=datetime.now,
                          onupdate=datetime.now)
    
    searchs = sao.relationship('Search', backref='hosts')
    
    def createUrl(self, page, tag):
        """Create url to fetch json data.
        
        For more information read this:
        danbooru or moebooru: https://yande.re/help/api
        gelbooru: http://gelbooru.com/index.php?page=help&topic=dapi
        
        Args:
            page: page of post to retrieve.
            tag: tag of post to retrieve.
            
        Returns:
            A string of url to fetch post data.
        """
        
        if self.type == self.TYPE_GELBOORU:
            address = '%s/index.php' % self.url
            params = {'page': 'dapi', 's': 'post', 'q': 'index', 'limit': 100,
                      'tags': tag, 'pid': page - 1}
        else:
            address = '%s/%s.json' % (self.url, self.api)
            params = {'limit': 100, 'tags': tag, 'page': page}
            if self.username:
                #Username: julot
                params['login'] = self.username
                #Password: 'd776dbc92b534d442ab74e7183d13c8ec3176373'
                params['password_hash'] = self.password
        param = '&'.join(['%s=%s' % (key, params[key]) for key in params])
        return '%s?%s' % (address, param)
    